#include <jni.h>
#include <stdio.h>
#include "org_jsux_SerialPort_NativeSerial.h"
#include "impl.h"

JNIEXPORT jstring JNICALL Java_org_jsux_SerialPort_00024NativeSerial_findPorts
  (JNIEnv *env, jobject thisObj)
{
	char *ports = findPorts();
	jstring result = (*env)->NewStringUTF(env,ports);
	free(ports);
	return result;
}

JNIEXPORT jlong JNICALL Java_org_jsux_SerialPort_00024NativeSerial_openPort
  (JNIEnv *env, jobject thisObj, jstring port)
{
    const char *str = (*env)->GetStringUTFChars(env, port, 0);

    void *ci = openPort((char *)str);
    jlong result = (jlong)(long) ci;

    (*env)->ReleaseStringUTFChars(env, port, str);

    return result;
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_setBlock
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return setBlock(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_setNonblock
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return setNonblock(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_enableDTR
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return enableDTR(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_disableDTR
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return disableDTR(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_enableRTS
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return enableRTS(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_disableRTS
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return disableRTS(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_setParityNone
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return setParityNone(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_setParityOdd
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return setParityOdd(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_setParityEven
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return setParityEven(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_setStopBits1
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return setStopBits1(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_setStopBits2
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return setStopBits2(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_setDataBits5
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return setDataBits5(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_setDataBits6
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return setDataBits6(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_setDataBits7
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return setDataBits7(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_setDataBits8
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return setDataBits8(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_setFlowNone
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return setFlowNone(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_setFlowHW
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return setFlowHW(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_setFlowSW
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return setFlowSW(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_setBaud
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr, jint baud)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return setBaud(ptr,baud);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_flush
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return flush(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_closePort
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;
	return closePort(ptr);
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_readPort
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr, jbyteArray buf, jint len)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;

	jboolean isCopy;
	jbyte *b = (*env)->GetByteArrayElements(env, buf, &isCopy);

	int result = readPort(ptr,b,len);
	(*env)->ReleaseByteArrayElements(env, buf, b, 0);

	return result;
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_writePort
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr, jbyteArray buf, jint len)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;

	jboolean isCopy;
	jbyte *b = (*env)->GetByteArrayElements(env, buf, &isCopy);

	int result = writePort(ptr,b,len);
	(*env)->ReleaseByteArrayElements(env, buf, b, 0);

	return result;
}

JNIEXPORT jint JNICALL Java_org_jsux_SerialPort_00024NativeSerial_available
  (JNIEnv *env, jobject thisObj, jlong commInfoPtr)
{
	CommInfo *ptr = (CommInfo *)(long)commInfoPtr;

	return available(ptr);
}
